var totalPage = 0;
var totalCnt = 0;
var chargingStationStatusData = [];
var chargingStationData = [];
var webcamIndex = undefined;
var detailTimeout, detailIndex;
var stationStatusCharts;
var countData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	createTableHead("realTimeStatusThead", chargingStationStatusTable, turnPageSearch);
	scrollTableSuspend("scrollBody", "realTimeStatusTable", "realTimeStatusTable1", "realTimeStatusThead", "scrollHeight");
	setBaseUrl();
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	stationStatusCharts();
	search();
	setTimeout(hideLoading, 1000);
})
function getChargingStation(data) {
	chargingStationData = data;
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function resetSearch(){
	setTimeout(search, 500);
}
function stationStatusCharts(){
	stationStatusCharts = echarts.init(document.getElementById("stationEcharts"));
	stationStatusCharts.setOption(searchEcharts("vertical", "center", 50, 0, "统计信息", [{value: 0, name: "0", "itemStyle": echartStyle('#EAEAEA', false, false, '#EAEAEA')}]));
}
function searchChargingStationStatus(data){
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	chargingStationStatusData = data && data.chargingStationStatusList || [];
	createTableBody("realTimeStatusTbody", chargingStationStatusTable, chargingStationStatusData, true);

	if (data == "error" || data == 'timeout') {
		$("#ChargingStationID").empty();
		$("#ChargingStationID").append("<option value=''>请选择</option>");
		$("#communicationState").empty();
		$("#communicationState").append("<option value=''>请选择</option>");
	} else {
		if (chargingStationData && chargingStationData.length > 0) {
			if ($("#ChargingStationID option").size() == 1) {
				var length = chargingStationData.length;
				for (var i = 0; i < length; i++) {
					$("#ChargingStationID").append("<option value='" + chargingStationStatusData[i].chargingStationID + "'>" + chargingStationStatusData[i].chargingStationName + "</option>");
				}
			}
			if ($("#communicationState option").size() == 1) {
				$("#communicationState").append("<option value='1'>正常</option>");
				$("#communicationState").append("<option value='0'>断开</option>");
			}
		}
	}
	countData = [];
	countData.push({"value": data.totalOnlineCount || 0, 
		"name": '正常:' + (data.totalOnlineCount || 0), 
		"itemStyle": echartStyle('#1abc9c', true, true,'#1abc9c')
		});
	countData.push({"value": (data.totalOfflineCount || 0), 
		"name": '断开:'+ (data.totalOfflineCount || 0), 
		"itemStyle": echartStyle('#e4ae1e', true, true,'#e4ae1e')
		});
	option.series[0].data = countData;
	option.legend.data = ['正常:' + (data.totalOnlineCount || 0),'断开:' + (data.totalOfflineCount || 0)];
	stationStatusCharts.setOption(option, true);
	setTimeout(search, 2000);
}
function changeSelect() {
	showLoading();
	setTimeout(hideLoading, 2500);
}
function chargingStationName(num){
	window.parent.detalisDrop('chargingStation/chargingStationDetail', num, $("#currentPage").val(), "drownDown1", "" ,"chargingStationPage" ,"chargingStation");
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(chargingStationStatusData[num].chargingStationID);
}
function search() {
	$ajax('realTimeChargingStation', 'chargingStationStatusForm', searchChargingStationStatus, '', searchChargingStationStatus);
}
function turnPageSearch(){
	showLoading();
	setTimeout(hideLoading, 3000);
}
function details(index){
	window.parent.detalisSkip('chargingStation/chargingStationRealTimeDetail',index,$("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(chargingStationStatusData[index].chargingStationID);
}
